# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/9/22 14:12
# @Author  : Dell
# @File    : chat_memory.py
# @Software: PyCharm
# @Desc    :帮助LLM记住信息——记忆
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model_name='qwen-plus', base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", temperature=0.5,
                 api_key="sk-7b4f290b1a3e42a1a9a1957fa44eff37")
history = ChatMessageHistory()
history.add_ai_message("你好！")
history.add_user_message("中国的首都是哪里？")
print(f"打印ai消息和用户消息：{history}")
ai_response = llm.invoke(history.messages)
print(f"打印ai回复结果{ai_response.content}")
history.add_ai_message(ai_response.content)
print(f"打印所有历史对话消息{history}")